package com.gome.han.bigdata.mr._010_grouping_comparator;


import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author Hanpeng
 * @date 2020/12/1 14:34
 * @description:
 */
public class OrderBean implements WritableComparable<OrderBean> {
    private int orderId;		// 订单id
    private double price;		// 价格

    public OrderBean() {
        super();
    }

    public OrderBean(int orderId, double price) {
        this.orderId = orderId;
        this.price = price;
    }

    @Override
    public int compareTo(OrderBean bean) {
        int result;
        // 先按照定id升序排序，如果相同 按照价格降序排序

        if (orderId > bean.getOrderId()) {
            result = 1;
        } else if (orderId < bean.getOrderId()) {
            result = -1;
        } else {

            if (price > bean.getPrice()) {
                result = -1;
            } else if (price < bean.getPrice()) {
                result = 1;
            } else {
                result = 0;
            }
        }

        return result;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(orderId);
        out.writeDouble(price);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        orderId = in.readInt();
        price = in.readDouble();
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return orderId + "\t" + price;
    }
}
